package pub

import (
	"fmt"
	"wxsdk/miniapp/core"
	"wxsdk/miniapp/message/template/miniapp"
	"wxsdk/miniapp/message/template/mp"
	//"github.com/astaxie/beego"
)
const (
	templateSendURL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"
)

type UniformTemp struct {
	ToUser          string           `json:"touser"`          //接受者OpenID
	TemplateMessage *miniapp.TemplateMessage `json:"weapp_template_msg,omitempty"`
	Message         *mp.Message         `json:"mp_template_msg,omitempty"`
}

func Send(accessToken string, msg *UniformTemp) (msgID int64, err error) {
	//
	uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
	var result struct {
		core.Error
	}
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", uri, msg, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	return
}
